package com.yangguang.huahui.ui.activitys;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.yangguang.huahui.Config;
import com.yangguang.huahui.R;
import com.yangguang.huahui.baseadapter.BaseQuickAdapter;
import com.yangguang.huahui.entity.Nongzi;
import com.yangguang.huahui.mvp.presenter.NongziPresenter;
import com.yangguang.huahui.mvp.view.NongziView;
import com.yangguang.huahui.ui.adapters.NongziAdapter;
import com.yangguang.huahui.widget.ProgressActivity;

import butterknife.BindView;
import butterknife.OnClick;

public class NongziActivity extends BaseMvpActivity<NongziView, NongziPresenter> implements NongziView, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.tv_title)
    TextView tv_title;
    private int page;
    @BindView(R.id.mProgressActivity)
    ProgressActivity mProgressActivity;
    @BindView(R.id.mSwip)
    SwipeRefreshLayout mSwip;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    private LinearLayoutManager layoutManager;
    private NongziAdapter mAdapter;

    @OnClick({R.id.iv_back})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
    @Override
    protected int initLayoutId() {
        return R.layout.activity_nongzi;
    }

    @Override
    protected void initView() {
        tv_title.setText(getString(R.string.nongzi_list));
        mSwip.setColorSchemeResources(R.color.main_color, R.color.main_color, R.color.main_color);
        mSwip.setOnRefreshListener(this);
        layoutManager = new LinearLayoutManager(mContext);
        mRecyclerView.setLayoutManager(layoutManager);
        //实现首次自动显示加载提示
        mSwip.post(new Runnable() {
            @Override
            public void run() {
                mSwip.setRefreshing(true);
            }
        });
        mAdapter = new NongziAdapter(R.layout.nongzi_list_item, null, 1);
        //设置加载动画
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        //将适配器添加到RecyclerView
        mRecyclerView.setAdapter(mAdapter);
        //设置自动加载监听
        mAdapter.setOnLoadMoreListener(this);
        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                startActivity(new Intent(mContext, DetailActivity.class)
                        .putExtra("webUrl", Config.SUPPLY_DETAIL)
                        .putExtra("id", mAdapter.getItem(position).getId())
                        .putExtra("type", 10)
                        .putExtra("detail_type", "2"));
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected NongziPresenter initPresenter() {
        return new NongziPresenter();
    }

    @Override
    protected void fetchData() {
        mPresenter.getNongzi(page);
    }

    @Override
    public void onSuccess(Nongzi result) {
        if (result.getList() != null){
            if (page > 0){
                mAdapter.addData(result.getList());//新增数据
                mAdapter.notifyDataChangedAfterLoadMore(true);
            }else {
                mAdapter.setNewData(result.getList());
                mAdapter.openLoadMore(result.getList().size(),true);//设置是否可以下拉加载  以及加载条数
            }
        }else {
            if (page > 0) {
                //所有数据加载完成后显示
                try {
                    mAdapter.notifyDataChangedAfterLoadMore(false);
                    View view = getLayoutInflater().inflate(R.layout.layout_end_view, (ViewGroup) mRecyclerView.getParent(), false);
                    mAdapter.addFooterView(view);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else {
                toEmpty();
            }
        }
        mSwip.setRefreshing(false);
    }
    public void toEmpty(){
        mProgressActivity.showEmpty(getResources().getDrawable(R.mipmap.load_no_data),"暂无数据","努力收集中");
    }

    @Override
    public void onError() {
        mSwip.setRefreshing(false);
        toError();
    }
    public void toError() {
        mProgressActivity.showError(getResources().getDrawable(R.mipmap.load_error), getString(R.string.progressActivityErrorTitlePlaceholder),
                getString(R.string.progressActivityErrorContentPlaceholder), getString(R.string.progressActivityErrorButton), new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mProgressActivity.showContent();
                        //重试
                        onRefresh();
                    }
                });
    }

    @Override
    public void onRefresh() {
        page = 0;
        fetchData();
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        fetchData();
    }
}
